package flater.test.cases
{
import flater.utils.StringUtil;

import net.digitalprimates.fluint.tests.TestCase;
    
public class TestStringUtils extends TestCase
{
    /**
     *  Test Method for StringUtils.abbreviate()
     */
    public function testAbbreviate() : void
    {
        var lengths     : Array     = [ 1, 2, 3, 4, 5, 10, 15, 20 ];
        var testString    : String    = "testing abbreviate 123";
        
        assertEquals( 0, StringUtil.abbreviate( testString, 0 ).length );

        for each ( var len : int in lengths )
        {
            assertEquals( Math.max( len, 3 ), StringUtil.abbreviate( testString, len ).length );
        }
        
        assertEquals( 22, StringUtil.abbreviate( testString, 30 ).length );
        
    }

    /**
     *  Test Method for StringUtils.contains()
     */
    public function testContains() : void
    {
        var string         : String = "abc 123";
        var tSequence    : String = "abc";
        var fSequence    : String = "456";
        
        assertTrue( StringUtil.contains( string, tSequence ) );
        assertFalse( StringUtil.contains( string, fSequence ) );
        assertFalse( StringUtil.contains( string, null ) );
        assertFalse( StringUtil.contains( null, null ) );
    }

    /**
     *  Test Method for StringUtils.endsWith()
     */
    public function testEndsWith() : void
    {
        var string         : String = "123 abc";
        var tSuffix        : String = "abc";
        var fSuffix        : String = "456";
        var fSuffix2    : String = "ABC";
        
        assertTrue( StringUtil.endsWith( string, tSuffix ) );
        
        assertFalse( StringUtil.endsWith( string, fSuffix ) );
        assertFalse( StringUtil.endsWith( string, fSuffix2 ) );
        assertFalse( StringUtil.endsWith( null, fSuffix ) );
        assertFalse( StringUtil.endsWith( string, null ) );
        assertFalse( StringUtil.endsWith( null, null ) );
    }

    /**
     *  Test Method for StringUtils.endsWithIgnoreCase()
     */
    public function testEndsWithIgnoreCase() : void
    {
        var string         : String = "123 abc";
        var tSuffix        : String = "abc";
        var tSuffix2    : String = "ABC";
        var fSuffix        : String = "456";
        
        assertTrue( StringUtil.endsWithIgnoreCase( string, tSuffix ) );
        assertTrue( StringUtil.endsWithIgnoreCase( string, tSuffix2 ) );
        
        assertFalse( StringUtil.endsWithIgnoreCase( string, fSuffix ) );
        assertFalse( StringUtil.endsWithIgnoreCase( null, fSuffix ) );
        assertFalse( StringUtil.endsWithIgnoreCase( string, null ) );
        assertFalse( StringUtil.endsWithIgnoreCase( null, null ) );
    }

    /**
     *  Test Method for StringUtils.lpad()
     */
    public function testLpad() : void
    {
        var lengths        : Array     = [ 0, 1, 2, 3, 4 ];
        var length        : int     = 0;
        var string         : String = "test";
        var char        : String = ".";
        
        for each ( length in lengths )
        {
            assertEquals( string, StringUtil.lpad( string, length, char ) );
        }
        
        assertEquals( "." + string,    StringUtil.lpad( string, 5, char ) );
        assertEquals( ".." + string,   StringUtil.lpad( string, 6, char ) );
        assertEquals( "..." + string,  StringUtil.lpad( string, 7, char ) );
        assertEquals( "...." + string, StringUtil.lpad( string, 8, char ) );
        
        assertNull( StringUtil.lpad( null, 0 ) );
        assertNull( StringUtil.lpad( string, -1 ) );
        assertNull( StringUtil.lpad( string, 10, null ) );
    }

    /**
     *  Test Method for StringUtils.pad()
     */
    public function testPad() : void
    {
        var lengths        : Array     = [ 0, 1, 2, 3, 4 ];
        var length        : int     = 0;
        var string         : String = "test";
        var char        : String = ".";
        
        for each ( length in lengths )
        {
            assertEquals( string, StringUtil.pad( string, length, char ) );
        }
        
        assertEquals( "." + string,    StringUtil.pad( string, 5, char ) );
        assertEquals( ".." + string,   StringUtil.pad( string, 6, char ) );
        assertEquals( "..." + string,  StringUtil.pad( string, 7, char ) );
        assertEquals( "...." + string, StringUtil.pad( string, 8, char ) );
        
        assertNull( StringUtil.pad( null, 0 ) );
        assertNull( StringUtil.pad( string, -1 ) );
        assertNull( StringUtil.pad( string, 10, null ) );
    }

    /**
     *  Test Method for StringUtils.rpad()
     */
    public function testRpad() : void
    {
        var lengths        : Array     = [ 0, 1, 2, 3, 4 ];
        var length        : int     = 0;
        var string         : String = "test";
        var char        : String = ".";
        
        for each ( length in lengths )
        {
            assertEquals( string, StringUtil.rpad( string, length, char ) );
        }
        
        assertEquals( string + ".",    StringUtil.rpad( string, 5, char ) );
        assertEquals( string + "..",   StringUtil.rpad( string, 6, char ) );
        assertEquals( string + "...",  StringUtil.rpad( string, 7, char ) );
        assertEquals( string + "....", StringUtil.rpad( string, 8, char ) );
        
        assertNull( StringUtil.rpad( null, 0 ) );
        assertNull( StringUtil.rpad( string, -1 ) );
        assertNull( StringUtil.rpad( string, 10, null ) );
    }

    /**
     *  Test Method for StringUtils.startsWith()
     */
    public function testStartsWith() : void
    {
        var string         : String = "abcdefg";
        var tPrefix        : String = "abc";
        var fPrefix        : String = "ABC";
        var fPrefix2    : String = "acb";
        
        assertTrue( StringUtil.startsWith( string, tPrefix ) );
        
        assertFalse( StringUtil.startsWith( string, fPrefix ) );
        assertFalse( StringUtil.startsWith( string, fPrefix2 ) );
        assertFalse( StringUtil.startsWith( null, fPrefix ) );
        assertFalse( StringUtil.startsWith( string, null ) );
        assertFalse( StringUtil.startsWith( null, null ) );
    }

    /**
     *  Test Method for StringUtils.startsWithIgnoreCase()
     */
    public function testStartsWithIgnoreCase() : void
    {
        var string         : String = "abcdefg";
        var tPrefix        : String = "abc";
        var tPrefix2    : String = "ABC";
        var fPrefix        : String = "acb";
        
        assertTrue( StringUtil.startsWithIgnoreCase( string, tPrefix ) );
        assertTrue( StringUtil.startsWithIgnoreCase( string, tPrefix2 ) );
        
        assertFalse( StringUtil.startsWithIgnoreCase( string, fPrefix ) );
        assertFalse( StringUtil.startsWithIgnoreCase( null, fPrefix ) );
        assertFalse( StringUtil.startsWithIgnoreCase( string, null ) );
        assertFalse( StringUtil.startsWithIgnoreCase( null, null ) );
    }

    /**
     *  Test Method for StringUtils.trim()
     */
    public function testTrim() : void
    {
        var strings        : Array = [ "  abcd  ", "abcd   ", "   abcd" ];
        var trimmedString  : String = "abcd";
        
        for each ( var string : String in strings )
        {
            assertEquals( trimmedString, StringUtil.trim( string ) );
        }
    }


} //  end class
} //  end package